package auth

import (
	"github.com/lishimeng/app-starter"
	"github.com/lishimeng/app-starter/midware/auth"
	"github.com/lishimeng/app-starter/server"
	"github.com/lishimeng/app-starter/token"
	"github.com/lishimeng/app-starter/tool"
	"github.com/lishimeng/go-log"
)

type UserInfo struct {
	UserId   string   `json:"user_id,omitempty"`
	UserName string   `json:"user_name,omitempty"`
	Roles    []string `json:"roles,omitempty"`
	Buttons  []string `json:"buttons,omitempty"`
}

func getUserInfo(ctx server.Context) {
	var err error
	var resp app.ResponseWrapper

	log.Info("auth get user_info")
	payload := ctx.C.Values().Get(auth.UserInfoKey)
	jwt, ok := payload.(token.JwtPayload)
	if !ok {
		return
	}
	log.Debug("code:%s", jwt.Uid)
	account, err := getUserInfoByCode(jwt.Uid)
	if err != nil {
		resp.Code = tool.RespCodeError
		resp.Message = "未查到记录"
		ctx.Json(resp)
		return
	}
	var ui UserInfo
	ui.UserId = jwt.Uid
	ui.UserName = account.UserName
	ui.Roles = account.Roles
	ui.Buttons = account.Buttons
	resp.Data = ui
	resp.Code = 200
	ctx.Json(resp)
}

func getUserInfoByCode(uid string) (ui UserInfo, err error) {

	ui.UserId = uid
	ui.UserName = "wala wala"
	ui.Roles = []string{"Admin"}
	return
}
